package com.zyx.concurrentdemo.mytest.c03;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Yaxi.Zhang
 * @since 2021/10/13 10:01
 * desc: 测试两阶段终止
 */
@Slf4j(topic = "c.TwoPhaseTerminateDemo")
public class TwoPhaseTerminateDemo {

    public static void main(String[] args) {
        Thread t1 = new Thread("t1") {
            @Override
            public void run() {
                Thread current = Thread.currentThread();
                while (true) {
                    if (current.isInterrupted()) { // 打断后
                        log.debug("料理后事");
                        break;
                    } else { // 未打断
                        try {
                            // 无异常则执行监控记录
                            Thread.sleep(1000);
                            log.debug("执行监控记录...");
                        } catch (InterruptedException e) {
                            // 有异常需要打断标记
                            e.printStackTrace();
                            current.interrupt();
                        }
                    }
                }
            }
        };

        t1.start();

        try {
            Thread.sleep(5000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        t1.interrupt();

    }

}
